<?php
FLEA::loadClass('Controller_Base');

class Controller_Copyright extends Controller_Base
{
	
	function	actionRecordCopyright()
	{
		$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
		$copy		=	$_POST;
		
		
		
		if($_POST['Type'] == 0)
		{
			$re			=	strtotime($_POST["Registered"]);
			$red		=	getdate($re);
			$red['year']=	$red['year']+90;
			$copy['Dxpires']	=	$red['year'].'-'.$red['mon'].'-'.$red['mday'];
		}
		else if($_POST['Type'] == 1)
		{
			if($_POST['Death'] != '')
			{
				$re			=	strtotime($_POST["Death"]);
				$red		=	getdate($re);
				$red['year']=	$red['year']+35;
				$copy['Dxpires']	=	$red['year'].'-'.$red['mon'].'-'.$red['mday'];
			}
			else
			{
				$copy['Dxpires']	=	"";
			}
		}
		$result		=	$_modelCopyright->create($copy);
		$this->showResult($result>0?1:0);
	}

	
	function actionGetCopyrights()
	{
		$role	=	$_POST['Role'];
		$userID	=	$_POST['UserID'];

		$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
		$copyrights				=	$_modelCopyright->findAllCopyrights($userID,$role);
		if(is_array($copyrights))
		{
			$this->purse($copyrights,'copyrights',0);
		}
	}
	
	function actionGetBidCopyrights()
	{
		
		$firmID	=	$_POST['FirmID'];
		$firmID	=	$firmID==''?0:$firmID;
		
		$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
		$copyrights			=	$_modelCopyright->findBidCopyrights($firmID);
		
		if(is_array($copyrights))
		{
			$this->purse($copyrights,'copyrights',0);
		}
	}
	
	function actionGetCopyright()
	{
		$copyrightID	=	$_POST['CopyrightID'];
		$copyrightID	=	$copyrightID==''?0:$copyrightID;
		
		$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
		$copyright				=	$_modelCopyright->findCopyright($copyrightID,$_POST['UserID'],$_POST['Role']);
		
		if(is_array($copyright))
		{
			$this->purse($copyright,'copyright',0);
		}
	}
	
	function actionGetCopyrightActions()
	{
		$copyrightID	=	$_POST['CopyrightID'];
		$copyrightID	=	$copyrightID==''?0:$copyrightID;
		
		$_modelCopyrightAction	=&	FLEA::getSingleton('Model_CopyrightAction');
		$actions				=	$_modelCopyrightAction->findAll('CopyrightID='.$copyrightID);
	
		print '<actions label="actions">';
		for($i=0; $i<count($actions) ; $i++)
		{
			print '<action
				label="'.$GLOBALS['ActionType'][$actions[$i]['Type']].'   [ Date:'.$actions[$i]['Date'] .']"
				ActionID="'.$actions[$i]['ActionID'].'"
				Cost="'.$actions[$i]['Cost'].'"
				Type="'.$actions[$i]['Type'].'"
				Date="'.$actions[$i]['Date'].'"
				Description="'.$actions[$i]['Description'].'"
				DueDate="'.$actions[$i]['DueDate'].'">';
			for($j=0; $j<count($actions[$i]['descriptions']); $j++)
			{


				print '<File label="';
				print substr($actions[$i]['descriptions'][$j]['Drawing0'],8,strlen($actions[$i]['descriptions'][$j]['Drawing0'])-8);
				print '"></File>';
			}
			print '</action>';
		}
		print '</actions>';
	
	}
	
	function actionAssignCopyrightToFirm()
	{
		$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
		
		$copy	=	$_POST;
		$copy['DeadlineDate']	=	date("Y-m-d",strtotime($_POST['DeadlineDate']));
		$copy['BConfirm']		=	0;
		$result	=	$_modelCopyright->save($copy);
		$this->showResult($result);
	}
	
	function	actionAcceptAssignCopyright()
	{
		$_modelCopyright	=&	FLEA::getSingleton('Model_Copyright');
		
		$copy['CopyrightID']	=	$_POST['CopyrightID'];
		$copy['BConfirm']		=	$_POST['BConfirm'];
		$copy['FirmID']			=	$_POST['FirmID'];
		
		$result	=	$_modelCopyright->save($copy);
		
		if($copy['BConfirm'] == 1)
		{
			$_modelActionEvent	=&	FLEA::getSingleton("Model_ActionEvent");
			$_modelActionEvent->updateActionEvent($_POST['FirmID'],$_POST['ClientID'],"Accept copyright");
		}
		
		$this->showResult($result);
	}
	
	function actionAddAction()
	{
		$_modelCopyright		=&	FLEA::getSingleton('Model_Copyright');
		$_modelCopyrightAction	=&	FLEA::getSingleton('Model_CopyrightAction');
		$_modelDescription		=&	FLEA::getSingleton('Model_Description');

		$action1['Title']				=	$_POST['Title']!=''?$_POST['Title']:'';
		$action1['Type']				=	$_POST['Type'];
		$action1['Description']			=	$_POST['Description'];
		$action1['CopyrightID']			=	$_POST['CopyrightID']==''?0:$_POST['CopyrightID'];
		$action1['Date']				=	date("Y-m-d", time());
		$action1['Cost']				=	$_POST['Cost'];

		$actionID	=	$_modelCopyrightAction->create($action1);

		if($_POST['FileNames'] != "")
		{
			$files	=	split("-",$_POST['FileNames']);

			for($k=0; $k<count($files); $k++)
			{
				if($files[$k] != "")
				{
					$description['ActionID']	=	$actionID;
					$description['Date']		=	date("Y-m-d", time());
					$description['Drawing0']	=	"Drawing/".$files[$k];
					$_modelDescription->create($description);
				}
			}
		}
		$copy['CopyrightID']	=	$action1['CopyrightID'];
		$copy['LastActionDate']=	$action1['Date'];
		$_modelCopyright->save($copy);
		$this->showResult($actionID>0?1:0);
	}
}
?>